package com.snack.common.utils;


import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

/**
 * 
 * 动态生成html操作类. <br>
 * Description.
 * <p>
 * Copyright: Copyright (c) Apr 7, 2009 11:37:10 AM
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: JinLei
 * <p>
 * Version: 1.0
 * <p>
 */
public class HtmlUtil {

	public static final Logger logger = Logger.getLogger(HtmlUtil.class);

	// Radio
	public static String getRadioHtml(Map<String, String> hm, String name,
			String value) {
		StringBuffer strBuff = new StringBuffer();
		Set<String> keySet = hm.keySet();
		Iterator<String> iter = keySet.iterator();
		int inx = 0;
		while (iter.hasNext()) {
			String key = String.valueOf(iter.next());
			strBuff.append("<input type='radio' name='").append(name)
					.append("' value='");
			strBuff.append(key);
			strBuff.append("' ");
			if (value.equals(key) || (inx == 0)) {
				strBuff.append(" checked ");
			}
			strBuff.append("/>&nbsp;").append(hm.get(key));

			inx++;
		}
		return strBuff.toString();
	}

	// Select
	public static String getSelectHtml(Map<String, String> hm, String name,
			String value) {
		return HtmlUtil.getSelectHtml(hm, name, value, "width:200px;");
	}

	public static String getSelectHtml(Map<String, String> hm, String name,
			String value, String style) {
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<select name='").append(name)
				.append("'  style=\"" + style + "\">");
		strBuff.append("<option value=''>==请选择==</option>");
		Set<String> keySet = hm.keySet();
		Iterator<String> iter = keySet.iterator();
		while (iter.hasNext()) {
			String key = String.valueOf(iter.next());
			strBuff.append("<option value='");
			strBuff.append(key);
			strBuff.append("' ");

			if (value.equals(key)) {
				strBuff.append(" selected ");
			}

			strBuff.append(" >").append(hm.get(key));
			strBuff.append("</option>");
		}
		strBuff.append("</select>");
		return strBuff.toString();
	}

	public static String getSelectAllOptionsHtml(Map<String, String> hm,
			String value, boolean isAll) {
		StringBuffer strBuff = new StringBuffer();
		if (isAll) {
			strBuff.append("<option value=''>==请选择==</option>");
		}
		Set<String> keySet = hm.keySet();
		Iterator<String> iter = keySet.iterator();
		while (iter.hasNext()) {
			String key = String.valueOf(iter.next());
			strBuff.append("<option value='");
			strBuff.append(key);
			strBuff.append("' ");

			if (value.equals(key)) {
				strBuff.append(" selected ");
			}

			strBuff.append(" >").append(hm.get(key));
			strBuff.append("</option>");
		}
		return strBuff.toString();
	}

	public static String getSelectNoAllOptionsHtml(Map<String, String> hm,
			String name, String value) {
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<select name='").append(name).append("' >");
		Set<String> keySet = hm.keySet();
		Iterator<String> iter = keySet.iterator();
		while (iter.hasNext()) {
			String key = String.valueOf(iter.next());
			strBuff.append("<option value='");
			strBuff.append(key);
			strBuff.append("' ");

			if (value.equals(key)) {
				strBuff.append(" selected ");
			}

			strBuff.append(" >").append(hm.get(key));
			strBuff.append("</option>");
		}
		strBuff.append("</select>");
		return strBuff.toString();
	}

	// CheckBox
	public static String getCheckBoxHtml(Map<String, String> hm, String name,
			Set<String> cbSetValue) {
		StringBuffer strBuff = new StringBuffer();

		Set<String> keySet = hm.keySet();
		Iterator<String> iter = keySet.iterator();

		int clos = 1; // 计算一行中的列数

		while (iter.hasNext()) {
			String key = iter.next();
			String value = String.valueOf(hm.get(key));
			strBuff.append("<input name='").append(name)
					.append("' type='checkbox' value='").append(key)
					.append("' ");
			if (cbSetValue.contains(key)) {
				strBuff.append(" checked ");
			}
			strBuff.append(" />").append(value).append("&nbsp;&nbsp;");

			if (clos == 8) {
				clos = 0;
				strBuff.append("</br>");
			}
			clos++;
		}

		return strBuff.toString();
	}

	// CheckBox
	public static String getCheckBoxHtml(Map<String, String> hm, String name,
			Set<String> cbSetValue, int count) {
		StringBuffer strBuff = new StringBuffer();

		Set<String> keySet = hm.keySet();
		Iterator<String> iter = keySet.iterator();

		int clos = 1; // 计算一行中的列数

		while (iter.hasNext()) {
			String key = iter.next();
			String value = String.valueOf(hm.get(key));
			strBuff.append("<input name='").append(name)
					.append("' type='checkbox' value='").append(key)
					.append("' ");
			if (cbSetValue.contains(key)) {
				strBuff.append(" checked ");
			}
			strBuff.append(" />").append(value).append("&nbsp;&nbsp;");

			if (clos == count) {
				clos = 0;
				strBuff.append("</br>");
			}
			clos++;
		}

		return strBuff.toString();
	}

	// CheckBox 显示
	public static String getCheckBoxTableHtml(Map<String, String> hm,
			String name, Set<String> cbSetValue, int count) {
		StringBuffer strBuff = new StringBuffer();
		Set<String> keySet = hm.keySet();
		Iterator<String> iter = keySet.iterator();
		int clos = 0; // 计算一行中的列数
		strBuff.append("<table width='100%'>");
		while (iter.hasNext()) {
			String key = iter.next();
			String value = String.valueOf(hm.get(key));
			if (clos == 0) {
				strBuff.append("<tr>");
			}
			strBuff.append("<td width='").append(100 / count).append("%'>");
			strBuff.append("<input name='").append(name)
					.append("' type='checkbox' value='").append(key)
					.append("' ");
			if (cbSetValue.contains(key)) {
				strBuff.append(" checked ");
			}
			strBuff.append(" />").append(value).append("&nbsp;&nbsp;");
			strBuff.append("</td>");
			if (clos == (count - 1)) {
				clos = 0;
				strBuff.append("</tr>");
			} else {
				clos++;
			}
		}
		strBuff.append("</table>");
		return strBuff.toString();
	}

	// CheckBox 显示
	public static String getCheckBoxTableViewHtml(Map<String, String> hm,
			int count) {
		StringBuffer strBuff = new StringBuffer();
		Set<String> keySet = hm.keySet();
		Iterator<String> iter = keySet.iterator();
		int clos = 0; // 计算一行中的列数
		strBuff.append("<table width='100%'>");
		while (iter.hasNext()) {
			String key = iter.next();
			String value = String.valueOf(hm.get(key));
			if (clos == 0) {
				strBuff.append("<tr>");
			}
			strBuff.append("<td width='").append(100 / count).append("%'>");
			strBuff.append(value).append("&nbsp;&nbsp;");
			strBuff.append("</td>");
			if (clos == (count - 1)) {
				clos = 0;
				strBuff.append("</tr>");
			} else {
				clos++;
			}
		}
		strBuff.append("</table>");
		return strBuff.toString();
	}
}